$(function(){
        window.requestAnimFrame=(function(){
              return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(callback){window.setTimeout(callback,1000/60)}
        })();
              var totalDeg=360*3+0;
              var steps=[];
              var lostDeg=[54,126,198,270,342];
              var prizeDeg=[18,90,162,234,306];
              var prize,sncode;
              var count=0;
              var now=0;
              var a=0.01;
              var outter,inner,timer,running=false;
         function countSteps()
         {
            var t=Math.sqrt(2*totalDeg/a);
            var v=a*t;
            for(var i=0;i<t;i++){
               steps.push((2*v*i-a*i*i)/2)
            }
            steps.push(totalDeg)
          }
          function step(){
             outter.style.webkitTransform='rotate('+steps[now++]+'deg)';
             outter.style.MozTransform='rotate('+steps[now++]+'deg)';
             if(now<steps.length){
                requestAnimFrame(step)
              }else{
               running=false;
               setTimeout(
                   function(){
                     if(prize!=null){
                          $("#sncode").text(sncode);
                          var type="";
                          if(prize==1){type="中奖了"}
                          $("#prizetype").text(type);
                          $("#result").slideToggle(500);
                          $("#outercont").slideUp(500)
                      }else{
                          count++;
                          alert("谢谢您的参与，下次再接再厉!")
                      }},200)}
          }
          function start(deg){
            deg=deg||lostDeg[parseInt(lostDeg.length*Math.random())];
            running=true;
            clearInterval(timer);
            totalDeg=360*5+deg;
            steps=[];
            now=0;
            countSteps();
            requestAnimFrame(step)
          }
          window.start=start;
          outter=document.getElementById('outer');
          inner=document.getElementById('inner');
          i=10;
          $("#inner").click(function(){
             if(running)return;
             if(count==1){return}
             if(count>1){alert("您已经抽过奖了。");return}
             if(prize!=null){alert("亲，你不能再参加本次活动了喔！下次再来吧~");return}
          $.ajax({
             url:"index.php?m=default&c=lottery&a=lotteryajax",
             dataType:"json",
             data:{ac:"activityuser",t:Math.random()},
             beforeSend:function(){
                running=true;
                timer=setInterval(
                   function(){
                         i+=5;
                         outter.style.webkitTransform='rotate('+i+'deg)';
                         outter.style.MozTransform='rotate('+i+'deg)'},1)},
             success:function(data){
                if(data.error=="limit"){
                  prize=null;start()
                  clearInterval(timer);
                  running=false;
          return;
                }
                if(data.error=="invalid"){
                  running=false;
                  clearInterval(timer);
                     setTimeout('alert("您已经抽过奖。");',1000);
                     count=1;
                     return
                }
                if(data.error=="noopenid"){
                  running=false;
                  clearInterval(timer);
                     setTimeout('alert("系统出错，请退出重新进入");',1000);
                     count=1;
                     return
                }
                if(data.error=="getsn"){
                  running=false;
                  clearInterval(timer);
                    setTimeout("alert('本次活动你已经中过奖，本次只显示你上次抽奖结果!兑奖SN码为:'"+data.sn+");",1000);
                    count=1;
                    prize=data.prizetype;
                    sncode=data.sn;
                    start(prizeDeg[data.prizetype-1]);return
                }
                if(data.success){
                  running=false;
                    prize=data.prizetype;
                    sncode=data.sn;
                    start(prizeDeg[data.prizetype-1])
                }else{
                  running=false;
                    prize=null;start()
                }

                count++
              },
              error:function(){prize=null;start();running=false;count++},timeout:4000})
              })
            });
            $("#save-btn").bind("click",function(){
                var btn=$(this);
                var tel=$("#tel").val();
                if(tel==''){alert("请输入手机号码");return}
                var regu=/^[1][0-9]{10}$/;var re=new RegExp(regu);if(!re.test(tel)){alert("请输入正确手机号码");return}
                var submitData={tid:5,code:$("#sncode").text(),tel:tel,action:"setTel"};
                $.post('index.php?ac=activityuser',submitData,function(data){if(data.success==true){alert("提交成功，谢谢您的参与");return}else{}},"json")
            });
 